package com.qidi.service.user.controller;

import com.qidi.service.user.entity.Department;
import com.qidi.service.user.entity.Role;
import com.qidi.service.user.entity.User;
import com.qidi.service.user.service.DepartmentRepository;
import com.qidi.service.user.service.RoleRepository;
import com.qidi.service.user.service.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 包    名 ：com.qidi.service.user.controller
 * 文 件 名 : UserController
 * 描    述 : TODO
 * 作    者 ：zhouchao超哥
 * 创建时间 ：2018/6/25 14:04
 * 版    本 ：1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserRepository userRepository;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    DepartmentRepository departmentRepository;


    @RequestMapping("/addData")
    public  void  addData(){

        departmentRepository.deleteAll();
        roleRepository.deleteAll();
        userRepository.deleteAll();

        Department d = new Department();
        d.setName("开发部");
        departmentRepository.save(d);

        Role r = new Role();
        r.setName("部门经理");
        roleRepository.save(r);
        List<Role> roles = new ArrayList<Role>();
        roles.add(r);

        User u = new User();
        u.setUsername("蔡智法");
        u.setCreateDate(new Date());
        u.setDepartment(d);
        u.setRoles(roles);
        u.setFlag("0");
        userRepository.save(u);
    }


    @GetMapping("/findByUsernameLike")
    public   List<User>  findByUsernameLike(){
         List<User> users =  userRepository.findByUsernameLike("蔡智法");
         return users;
    }


}
